/*
    Copyright (C) 2008 Edwin Lee

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

var REFRESH_INTERVAL_MS = 1000;
var FADEINOUT_INTERVAL_MS = 10;
var FADEINOUT_OPACITY_STEP = 5;

var photoShow = null;

PhotoShow = function()
{
	var photoShow = new Object();
	
	photoShow.flickrUserName = null;
	photoShow.size = DEFAULT_SIZE;
	photoShow.hAlign = DEFAULT_HALIGN;
	photoShow.vAlign = DEFAULT_VALIGN;
	photoShow.refreshInterval = DEFAULT_REFRESH_INTERVAL_SEC;
	photoShow.fadeTransition = false;
	photoShow.optionsChanged = false;
	photoShow.doReadUserPhotos = false;
	
	photoShow.user = null;
	
	photoShow.photos = new Array();
	
	var container = view.children.item("container");
	var photoBoard = container.children.item("photoBoard");
	
	photoShow.imgActive = photoBoard.children.item("imgPhoto1");
	photoShow.imgInactive = photoBoard.children.item("imgPhoto2");
	photoShow.width = 0;
	photoShow.height = 0;
	photoShow.fadeInOutToken = 0;
	
	photoShow.refreshCounter = 0;
	
	photoShow.addPhoto = function(photo)
	{
		this.photos[this.photos.length] = photo;
	}
	
	photoShow.refresh = function(view)
	{
		this.optionsChanged = options.exists(KEY_OPTIONS_CHANGED);
		
		if ((this.refreshCounter >= this.refreshInterval) || this.optionsChanged)
		{
			if (this.optionsChanged)
			{
				this.readOptions();
				options.remove(KEY_OPTIONS_CHANGED);
				
				this.setAllSizes();
			}
			
			if (this.doReadUserPhotos)
			{
				this.readUserPhotos();
			}
			
			if (this.photos.length > 0)
			{
				var index = Math.floor(Math.random() * this.photos.length);
				
				if ((this.photos[index].id) && (this.photos[index].secret) && (this.photos[index].server) && (this.photos[index].farm))
				{
					var photoStream = getPhotoStream(this.photos[index].farm, this.photos[index].server, this.photos[index].id, this.photos[index].secret, this.size);
					
					if (photoStream)
					{
						this.imgInactive.src = photoStream;
						
						var srcWidth = this.imgInactive.srcWidth;
						var srcHeight = this.imgInactive.srcHeight;
						
						var xOffset;
						
						if (this.hAlign == "left")
						{
							xOffset = 0;
						}
						else if (this.hAlign == "right")
						{
							xOffset = this.width - srcWidth;
						}
						else
						{
							xOffset = (this.width - srcWidth) / 2;
						}
						
						var yOffset;
						
						if (this.vAlign == "top")
						{
							yOffset = 0;
						}
						else if (this.vAlign == "bottom")
						{
							yOffset = this.height - srcHeight;
						}
						else
						{
							yOffset = (this.height - srcHeight) / 2;
						}
						
						this.imgInactive.x = xOffset;
						this.imgInactive.y = yOffset;
						
						this.imgInactive.width = srcWidth;
						this.imgInactive.height = srcHeight;
						
						var imgTemp = this.imgActive;
						this.imgActive = this.imgInactive;
						this.imgInactive = imgTemp;
						
						var titleDiv = view.children.item("container").children.item("photoBoard").children.item("titleDiv");
						var labelTitle = titleDiv.children.item("labelTitle");
						titleDiv.x = xOffset;
						titleDiv.y = yOffset;
						titleDiv.width = srcWidth;
						titleDiv.height = srcHeight;
						labelTitle.width = srcWidth;
						labelTitle.height = srcHeight;
						labelTitle.innerText = this.photos[index].title;

						var photoPageUrl = getPhotoPageUrl(this.user.nsid, this.photos[index].id);
						var anchorImage = view.children.item("anchorImage");
						anchorImage.href = photoPageUrl;
						
						var photoBoard = view.children.item("container").children.item("photoBoard");
						photoBoard.background = "transparent";
						photoBoard.opacity = 255;
	
						if (this.fadeTransition)
						{
							this.fadeInOutToken = view.setInterval("photoShow.fadeInOut(photoShow.imgActive, photoShow.imgInactive);", FADEINOUT_INTERVAL_MS);
						}
						else
						{
							this.imgActive.opacity = 255;
							this.imgInactive.opacity = 0;
						}
					}
				}
			}
			
			this.refreshCounter = 0;
			this.optionsChanged = false;
		}
		
		this.refreshCounter++;
	}
	
	photoShow.readOptions = function()
	{
		if (options.exists(KEY_FLICKR_USER_NAME))
		{
			var newFlickrUserName = options.getValue(KEY_FLICKR_USER_NAME);
			
			if (newFlickrUserName != this.flickrUserName)
			{
				this.flickrUserName = newFlickrUserName;
				
				this.doReadUserPhotos = true;
			}
		}
		
		if (options.exists(KEY_SIZE))
		{
			this.size = options.getValue(KEY_SIZE);
		}
		
		if (this.size == VALUE_SIZE_THUMBNAIL)
		{
			this.width = SIZE_THUMBNAIL_WIDTH;
			this.height = SIZE_THUMBNAIL_HEIGHT;
		}
		else if (this.size == VALUE_SIZE_SMALL)
		{
			this.width = SIZE_SMALL_WIDTH;
			this.height = SIZE_SMALL_HEIGHT;
		}
		else if (this.size == VALUE_SIZE_MEDIUM)
		{
			this.width = SIZE_MEDIUM_WIDTH;
			this.height = SIZE_MEDIUM_HEIGHT;
		}
		
		if ((options.exists(KEY_HALIGN)) && (options.exists(KEY_VALIGN)))
		{
			this.hAlign = options.getValue(KEY_HALIGN);
			this.vAlign = options.getValue(KEY_VALIGN);
		}
		
		if (options.exists(KEY_REFRESH_INTERVAL_SEC))
		{
			this.refreshInterval = options.getValue(KEY_REFRESH_INTERVAL_SEC);
		}
		
		if (options.exists(KEY_FADE_TRANSITION))
		{
			this.fadeTransition = options.getValue(KEY_FADE_TRANSITION);
		}
	}
	
	photoShow.setAllSizes = function()
	{
		view.width = this.width;
		view.height = this.height;
		
		var container = view.children.item("container");
		container.width = this.width;
		container.height = this.height;
		
		var photoBoard = container.children.item("photoBoard");
		photoBoard.width = this.width;
		photoBoard.height = this.height;
		
		if (this.imgActive.width > this.width)
		{
			this.imgActive.width = this.width;
		}
		if (this.imgActive.height > this.height)
		{
			this.imgActive.height = this.height;
		}
		
		var titleDiv = photoBoard.children.item("titleDiv");
		titleDiv.y = this.height - 15;
		titleDiv.width = this.width;
		
		var labelTitle = titleDiv.children.item("labelTitle");
		labelTitle.width = this.width;
		labelTitle.height = this.height;
		
		var anchorImage = view.children.item("anchorImage");
		anchorImage.width = this.width;
		anchorImage.height = this.height;
	}
	
	photoShow.readUserPhotos = function()
	{
		if (this.flickrUserName)
		{
			this.user = getUserByUsername(FLICKR_API_KEY, this.flickrUserName);
			
			if (this.user)
			{
				this.photos = getUserPhotos(FLICKR_API_KEY, this.user.nsid, INITIAL_LOAD_METADATA_SIZE);
				
				view.setTimeout("photoShow.readUserPhotosAsync();", (FULL_METADATA_LOAD_OFFSET_SEC * 1000));
			}
		}
	}
	
	photoShow.readUserPhotosAsync = function()
	{
		var photosTemp = getUserPhotos(FLICKR_API_KEY, this.user.nsid, -1);
		
		if (photosTemp && (photosTemp.length > 0))
		{
			this.doReadUserPhotos = false;
			
			this.photos = photosTemp;
		}
	}
	
	photoShow.fadeInOut = function(imgFadeIn, imgFadeOut)
	{
		imgFadeIn.opacity += FADEINOUT_OPACITY_STEP;
		imgFadeOut.opacity -= FADEINOUT_OPACITY_STEP;
		
		if ((imgFadeIn.opacity >= 255) || (imgFadeOut.opacity <= 0))
		{
			view.clearInterval(this.fadeInOutToken);
		}
	}
	
	photoShow.showTitle = function()
	{
		var titleDiv = view.children.item("container").children.item("photoBoard").children.item("titleDiv");
		var labelTitle = titleDiv.children.item("labelTitle");
		
		titleDiv.background = "#FFFFFF";
		titleDiv.opacity = "191";
		labelTitle.opacity = "255";
	}
	
	photoShow.hideTitle = function()
	{
		var titleDiv = view.children.item("container").children.item("photoBoard").children.item("titleDiv");
		var labelTitle = titleDiv.children.item("labelTitle");
		
		titleDiv.background = "transparent";
		titleDiv.opacity = "0";
		labelTitle.opacity = "0";
	}
	
	return photoShow;
}

onOpen = function()
{
	if (!options.exists(KEY_FLICKR_USER_NAME))
	{
		options.putValue(KEY_FLICKR_USER_NAME, DEFAULT_FLICKR_USER_NAME);
	}
	
	if (!options.exists(KEY_SIZE))
	{
		options.putValue(KEY_SIZE, DEFAULT_SIZE);
	}
	
	if (!options.exists(KEY_HALIGN))
	{
		options.putValue(KEY_HALIGN, DEFAULT_HALIGN);
	}
	
	if (!options.exists(KEY_VALIGN))
	{
		options.putValue(KEY_VALIGN, DEFAULT_VALIGN);
	}
	
	if (!options.exists(KEY_REFRESH_INTERVAL_SEC))
	{
		options.putValue(KEY_REFRESH_INTERVAL_SEC, DEFAULT_REFRESH_INTERVAL_SEC);
	}
	
	if (!options.exists(KEY_FADE_TRANSITION))
	{
		options.putValue(KEY_FADE_TRANSITION, DEFAULT_FADE_TRANSITION);
	}
	
	photoShow = PhotoShow();
	
	options.putValue(KEY_OPTIONS_CHANGED, 1);
	
	photoShow.refresh(view);
	
	view.setInterval("photoShow.refresh(view);", REFRESH_INTERVAL_MS);
}
onMouseOver = function()
{
	if (photoShow)
	{
		photoShow.showTitle();
	}
}

onMouseOut = function()
{
	if (photoShow)
	{
		photoShow.hideTitle();
	}
}

